package com.zhn.evsi.interceptor;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestResponse;
import com.dtflys.forest.interceptor.Interceptor;
import com.zhn.evsi.common.Result;
import com.zhn.evsi.domain.dto.System.ErrorDTO;
import com.zhn.evsi.domain.dto.System.ResultDto;
import com.zhn.evsi.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 无token拦截器
 * */
@Slf4j
@Component
public class TokenExceptionInterceptor implements Interceptor<ErrorDTO> {

    @Override
    public void onError(ForestRuntimeException exception, ForestRequest request, ForestResponse response) {
        int statusCode = response.getStatusCode();
        String content = response.getContent();
        Object result = response.getResult();


        if (response.getStatusCode() != 200 && response.getContent() != null) {
            String errorMsg = JSONObject.parseObject(content).get("error").toString();
            throw new CustomException(errorMsg,statusCode);
        }
    }



}
